/*
 * DownloadServlet.java
 *
 * Created on 1 de noviembre de 2006, 02:48 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.radar.download.servlet;

/**
 *
 * @author jlperea
 */
// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 31/10/2006 06:13:45 p.m.
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 

import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadServlet extends HttpServlet
{
    private static final String DIR = "dir";
    private static final String GZIP = "gzip";
    private static final String MIME = "mime";
    private static final String ERROR = "error";
    private ServletContext context;
    private String separator;
    private String root;
    private String error;
    private static final String VERSION = "";
    private static final String CPR = "";
    
    boolean debeEntrarAZip = false;
    
    public DownloadServlet()
    {
        separator = "/";
        root = ".";
        error = null;
    }

    public void init(ServletConfig servletconfig)
        throws ServletException
    {
        super.init(servletconfig);
        context = servletconfig.getServletContext();
        error = getInitParameter(ERROR);
        
        String s;
        if((s = getInitParameter(DIR)) == null)
        {
            s = root;
        }
        
        separator = System.getProperty("file.separator");
        if(!s.endsWith(separator) && !s.endsWith("/"))
        {
            s = s + separator;
        }
        root = s;
    }

    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        doPost(httpservletrequest, httpservletresponse);
    }

    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        debeEntrarAZip = false;
        
        Object obj = null;
        String s = "";
        s = HttpUtils.getRequestURL(httpservletrequest).toString();
        
        int i;
        if((i = s.indexOf("?")) > 0)
        {
            s = s.substring(0, i);
        }
        
        String s1;
        if((s1 = httpservletrequest.getQueryString()) == null)
        {
            s1 = "";
        }
        else
        {
            s1 = decode(s1);
        }
        
        if(s1.length() == 0)
        {
            if(error != null)
            {
                if(error.startsWith("http://"))
                {
                    httpservletresponse.sendRedirect(httpservletresponse.encodeRedirectURL(error));
                } 
                else
                {
                    RequestDispatcher requestdispatcher = getServletConfig().getServletContext().getRequestDispatcher(error);
                    requestdispatcher.forward(httpservletrequest, httpservletresponse);
                }
            } 
            else
            {
                httpservletresponse.setContentType("text/html");
                PrintWriter printwriter = httpservletresponse.getWriter();
                printwriter.println("<html>");
                printwriter.println("<br><br><br>No se pudo descargar el archivo, el archivo puede haber sido borrado ");
                printwriter.println("</html>");
                printwriter.flush();
                printwriter.close();
            }
            return;
        }
        if(s1.indexOf(".." + separator) >= 0 || s1.indexOf("../") >= 0)
        {
            if(error != null)
            {
                if(error.startsWith("http://"))
                {
                    httpservletresponse.sendRedirect(httpservletresponse.encodeRedirectURL(error));
                } else
                {
                    RequestDispatcher requestdispatcher1 = getServletConfig().getServletContext().getRequestDispatcher(error);
                    requestdispatcher1.forward(httpservletrequest, httpservletresponse);
                }
            } 
            else
            {
                httpservletresponse.setContentType("text/html");
                PrintWriter printwriter1 = httpservletresponse.getWriter();
                printwriter1.println("<html>");
                printwriter1.println("<br><br><br>No se pudo descargar el archivo por cuestiones de seguridad ");
                printwriter1.println("</html>");
                printwriter1.flush();
                printwriter1.close();
            }
            return;
        }
        
        File file = lookupFile(root + s1);
        if(file == null)
        {
            if(error != null)
            {
                if(error.startsWith("http://"))
                {
                    httpservletresponse.sendRedirect(httpservletresponse.encodeRedirectURL(error));
                } 
                else
                {
                    RequestDispatcher requestdispatcher2 = getServletConfig().getServletContext().getRequestDispatcher(error);
                    requestdispatcher2.forward(httpservletrequest, httpservletresponse);
                }
            } 
            else
            {
                httpservletresponse.setContentType("text/html");
                PrintWriter printwriter2 = httpservletresponse.getWriter();
                printwriter2.println("<html>");
                printwriter2.println("<br><br><br>No se pudo descargar el archivo, el archivo puede haber sido borrado ");
                printwriter2.println("</html>");
                printwriter2.flush();
                printwriter2.close();
            }
            return;
        }
        
        if(!file.exists() || !file.canRead())
        {
            if(error != null)
            {
                if(error.startsWith("http://"))
                {
                    httpservletresponse.sendRedirect(httpservletresponse.encodeRedirectURL(error));
                } 
                else
                {
                    RequestDispatcher requestdispatcher3 = getServletConfig().getServletContext().getRequestDispatcher(error);
                    requestdispatcher3.forward(httpservletrequest, httpservletresponse);
                }
            } 
            else
            {
                httpservletresponse.setContentType("text/html");
                PrintWriter printwriter3 = httpservletresponse.getWriter();
                printwriter3.println("<html><font face=\"Arial\">");
                printwriter3.println("<br><br><br>No se pudo leer el archivo " + s1);
                printwriter3.print("<br>Posible Causa: ");
                if(!file.exists())
                {
                    printwriter3.println("el archivo puede haber sido borrado");
                }
                else
                {
                    printwriter3.println("error de lectura del archivo");
                }
                printwriter3.println("</font></html>");
                printwriter3.flush();
                printwriter3.close();
            }
            return;
        }
        
        String s2 = httpservletrequest.getHeader("Accept-Encoding");        
        
        
        String mimeType = getMimeType(s1);       
        
        if(debeEntrarAZip)
        {
            httpservletresponse.setHeader("Content-Encoding", GZIP);
            httpservletresponse.setHeader("Content-Disposition", "attachment;filename=\"" + s1 + "\"");
            javax.servlet.ServletOutputStream servletoutputstream = httpservletresponse.getOutputStream();
            GZIPOutputStream gzipoutputstream = new GZIPOutputStream(servletoutputstream);
            dumpFile(root + s1, gzipoutputstream);
            gzipoutputstream.close();
            servletoutputstream.close();
        }
        else
        {
            httpservletresponse.setContentType(mimeType);
            
            httpservletresponse.setHeader("Content-Disposition", "attachment;filename=\"" + s1 + "\"");
            httpservletresponse.setContentLength((int)file.length());
            javax.servlet.ServletOutputStream servletoutputstream1 = httpservletresponse.getOutputStream();

            dumpFile(root + s1, servletoutputstream1);

            servletoutputstream1.flush();
            servletoutputstream1.close();
        }        
    }

    private String getFromQuery(String s, String s1)
    {
        if(s == null)
        {
            return "";
        }
        
        int i = s.indexOf(s1);
        
        if(i < 0)
        {
            return "";
        }
        
        String s2 = s.substring(i + s1.length());
        
        if((i = s2.indexOf("&")) < 0)
        {
            return s2;
        }
        else
        {
            return s2.substring(0, i);
        }
    }

    private void dumpFile(String s, OutputStream outputstream)
    {
        String s1 = s;
        byte abyte0[] = new byte[4096];
        
        try
        {
            BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(lookupFile(s1)));
            int i;
            while((i = bufferedinputstream.read(abyte0, 0, 4096)) != -1) 
                outputstream.write(abyte0, 0, i);
            bufferedinputstream.close();
        }
        catch(Exception exception) 
        {
            exception.printStackTrace();
        }
    }

    private String decode(String s)
    {
        StringBuffer stringbuffer = new StringBuffer(0);
        
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            if(c == '+')
            {
                stringbuffer.append(' ');
                continue;
            }
            if(c == '%')
            {
                char c1 = '\0';
                for(int j = 0; j < 2; j++)
                {
                    c1 *= '\020';
                    c = s.charAt(++i);
                    if(c >= '0' && c <= '9')
                    {
                        c1 += c - 48;
                        continue;
                    }
                    if((c < 'A' || c > 'F') && (c < 'a' || c > 'f'))
                        break;
                    switch(c)
                    {
                    case 65: // 'A'
                    case 97: // 'a'
                        c1 += '\n';
                        break;

                    case 66: // 'B'
                    case 98: // 'b'
                        c1 += '\013';
                        break;

                    case 67: // 'C'
                    case 99: // 'c'
                        c1 += '\f';
                        break;

                    case 68: // 'D'
                    case 100: // 'd'
                        c1 += '\r';
                        break;

                    case 69: // 'E'
                    case 101: // 'e'
                        c1 += '\016';
                        break;

                    case 70: // 'F'
                    case 102: // 'f'
                        c1 += '\017';
                        break;
                    }
                }

                stringbuffer.append(c1);
            } 
            else
            {
                stringbuffer.append(c);
            }
        }

        return stringbuffer.toString();
    }

    public String getServletInfo()
    {
        return "";
    }

    private File lookupFile(String s)
    {
        File file = new File(s);
        return file.isAbsolute() ? file : new File(context.getRealPath("/"), s);
    }

    private String getMimeType(String s)
    {
        int i = s.lastIndexOf(".");
        
        if(i > 0 && i < s.length() - 1)
        {
            String s1 = s.substring(i + 1);
            
            if(s1.equalsIgnoreCase("hqx"))
                return "application/mac-binhex40";
            if(s1.equalsIgnoreCase("cpt"))
                return "application/mac-compactpro";
            if(s1.equalsIgnoreCase("doc"))
                return "application/msword"; 
            if(s1.equalsIgnoreCase("xls"))
                return "application/vnd.ms-excel";
            if(s1.equalsIgnoreCase("mpp"))
                return "application/vnd.ms-project";
            if(s1.equalsIgnoreCase("ppt"))
                return "application/vnd.ms-powerpoint";
            if(s1.equalsIgnoreCase("pps"))
                return "application/vnd.ms-powerpoint";
            if(s1.equalsIgnoreCase("jsp"))
                return "application/jsp";
            if(s1.equalsIgnoreCase("oda"))
                return "application/oda";
            if(s1.equalsIgnoreCase("pdf"))
                return "application/pdf";
            if(s1.equalsIgnoreCase("ai"))
                return "application/postscript";
            if(s1.equalsIgnoreCase("eps"))
                return "application/postscript";
            if(s1.equalsIgnoreCase("ps"))
                return "application/postscript";           
            if(s1.equalsIgnoreCase("rtf"))
                return "application/rtf";
            if(s1.equalsIgnoreCase("bcpio"))
                return "application/x-bcpio";
            if(s1.equalsIgnoreCase("vcd"))
                return "application/x-cdlink";
            if(s1.equalsIgnoreCase("Z"))
                return "application/x-compress";
            if(s1.equalsIgnoreCase("cpio"))
                return "application/x-cpio";
            if(s1.equalsIgnoreCase("csh"))
                return "application/x-csh";
            if(s1.equalsIgnoreCase("dcr"))
                return "application/x-director";
            if(s1.equalsIgnoreCase("dir"))
                return "application/x-director";
            if(s1.equalsIgnoreCase("dxr"))
                return "application/x-director";
            if(s1.equalsIgnoreCase("dvi"))
                return "application/x-dvi";
            if(s1.equalsIgnoreCase("gtar"))
                return "application/x-gtar";
            if(s1.equalsIgnoreCase("gz"))
            {
                debeEntrarAZip = true;                
                return "application/x-gzip";
            }
            if(s1.equalsIgnoreCase("hdf"))
                return "application/x-hdf";
            if(s1.equalsIgnoreCase("cgi"))
                return "application/x-httpd-cgi";
            if(s1.equalsIgnoreCase("jnlp"))
                return "application/x-java-jnlp-file";
            if(s1.equalsIgnoreCase("skp"))
                return "application/x-koan";
            if(s1.equalsIgnoreCase("skd"))
                return "application/x-koan";
            if(s1.equalsIgnoreCase("skt"))
                return "application/x-koan";
            if(s1.equalsIgnoreCase("skm"))
                return "application/x-koan";
            if(s1.equalsIgnoreCase("latex"))
                return "application/x-latex";
            if(s1.equalsIgnoreCase("mif"))
                return "application/x-mif";
            if(s1.equalsIgnoreCase("nc"))
                return "application/x-netcdf";
            if(s1.equalsIgnoreCase("cdf"))
                return "application/x-netcdf";
            if(s1.equalsIgnoreCase("sh"))
                return "application/x-sh";
            if(s1.equalsIgnoreCase("shar"))
                return "application/x-shar";
            if(s1.equalsIgnoreCase("sit"))
                return "application/x-stuffit";
            if(s1.equalsIgnoreCase("sv4cpio"))
                return "application/x-sv4cpio";
            if(s1.equalsIgnoreCase("sv4crc"))
                return "application/x-sv4crc";
            if(s1.equalsIgnoreCase("tar"))
                return "application/x-tar";
            if(s1.equalsIgnoreCase("tcl"))
                return "application/x-tcl";
            if(s1.equalsIgnoreCase("tex"))
                return "application/x-tex";
            if(s1.equalsIgnoreCase("textinfo"))
                return "application/x-texinfo";
            if(s1.equalsIgnoreCase("texi"))
                return "application/x-texinfo";
            if(s1.equalsIgnoreCase("t"))
                return "application/x-troff";
            if(s1.equalsIgnoreCase("tr"))
                return "application/x-troff";
            if(s1.equalsIgnoreCase("roff"))
                return "application/x-troff";
            if(s1.equalsIgnoreCase("man"))
                return "application/x-troff-man";
            if(s1.equalsIgnoreCase("me"))
                return "application/x-troff-me";
            if(s1.equalsIgnoreCase("ms"))
                return "application/x-troff-ms";
            if(s1.equalsIgnoreCase("ustar"))
                return "application/x-ustar";
            if(s1.equalsIgnoreCase("src"))
                return "application/x-wais-source";
            if(s1.equalsIgnoreCase("xml"))
                return "text/xml";
            if(s1.equalsIgnoreCase("ent"))
                return "text/xml";
            if(s1.equalsIgnoreCase("cat"))
                return "text/xml";
            if(s1.equalsIgnoreCase("sty"))
                return "text/xml";
            if(s1.equalsIgnoreCase("dtd"))
                return "text/dtd";
            if(s1.equalsIgnoreCase("xsl"))
                return "text/xsl";
            if(s1.equalsIgnoreCase("zip"))
            {
                debeEntrarAZip = true;
                return "application/zip";
            }
            if(s1.equalsIgnoreCase("au"))
                return "audio/basic";
            if(s1.equalsIgnoreCase("snd"))
                return "audio/basic";
            if(s1.equalsIgnoreCase("mpga"))
                return "audio/mpeg";
            if(s1.equalsIgnoreCase("mp2"))
                return "audio/mpeg";
            if(s1.equalsIgnoreCase("mp3"))
                return "audio/mpeg";
            if(s1.equalsIgnoreCase("aif"))
                return "audio/x-aiff";
            if(s1.equalsIgnoreCase("aiff"))
                return "audio/x-aiff";
            if(s1.equalsIgnoreCase("aifc"))
                return "audio/x-aiff";
            if(s1.equalsIgnoreCase("ram"))
                return "audio/x-pn-realaudio";
            if(s1.equalsIgnoreCase("rpm"))
                return "audio/x-pn-realaudio-plugin";
            if(s1.equalsIgnoreCase("ra"))
                return "audio/x-realaudio";
            if(s1.equalsIgnoreCase("wav"))
                return "audio/x-wav";
            if(s1.equalsIgnoreCase("pdb"))
                return "chemical/x-pdb";
            if(s1.equalsIgnoreCase("xyz"))
                return "chemical/x-pdb";
            if(s1.equalsIgnoreCase("gif"))
                return "image/gif";
            if(s1.equalsIgnoreCase("ief"))
                return "image/ief";
            if(s1.equalsIgnoreCase("jpeg"))
                return "image/jpeg";
            if(s1.equalsIgnoreCase("jpg"))
                return "image/jpeg";
            if(s1.equalsIgnoreCase("jpe"))
                return "image/jpeg";
            if(s1.equalsIgnoreCase("png"))
                return "image/png";
            if(s1.equalsIgnoreCase("tiff"))
                return "image/tiff";
            if(s1.equalsIgnoreCase("tif"))
                return "image/tiff";
            if(s1.equalsIgnoreCase("ras"))
                return "image/x-cmu-raster";
            if(s1.equalsIgnoreCase("pnm"))
                return "image/x-portable-anymap";
            if(s1.equalsIgnoreCase("pbm"))
                return "image/x-portable-bitmap";
            if(s1.equalsIgnoreCase("pgm"))
                return "image/x-portable-graymap";
            if(s1.equalsIgnoreCase("ppm"))
                return "image/x-portable-pixmap";
            if(s1.equalsIgnoreCase("rgb"))
                return "image/x-rgb";
            if(s1.equalsIgnoreCase("xbm"))
                return "image/x-xbitmap";
            if(s1.equalsIgnoreCase("xpm"))
                return "image/x-xpixmap";
            if(s1.equalsIgnoreCase("xwd"))
                return "image/x-xwindowdump";
            if(s1.equalsIgnoreCase("html"))
                return "text/html";
            if(s1.equalsIgnoreCase("htm"))
                return "text/html";
            if(s1.equalsIgnoreCase("txt"))
                return "text/plain";
            if(s1.equalsIgnoreCase("java"))
                return "text/plain";
            if(s1.equalsIgnoreCase("class"))
                return "text/plain";
            if(s1.equalsIgnoreCase("rtx"))
                return "text/richtext";
            if(s1.equalsIgnoreCase("tsv"))
                return "text/tab-separated-values";
            if(s1.equalsIgnoreCase("etx"))
                return "text/x-setext";
            if(s1.equalsIgnoreCase("sgml"))
                return "text/x-sgml";
            if(s1.equalsIgnoreCase("sgm"))
                return "text/x-sgml";
            if(s1.equalsIgnoreCase("mpeg"))
                return "video/mpeg";
            if(s1.equalsIgnoreCase("mpg"))
                return "video/mpeg";
            if(s1.equalsIgnoreCase("mpe"))
                return "video/mpeg";
            if(s1.equalsIgnoreCase("qt"))
                return "video/quicktime";
            if(s1.equalsIgnoreCase("mov"))
                return "video/quicktime";
            if(s1.equalsIgnoreCase("avi"))
                return "video/x-msvideo";
            if(s1.equalsIgnoreCase("movie"))
                return "video/x-sgi-movie";
            if(s1.equalsIgnoreCase("ice"))
                return "x-conference/x-cooltalk";
            if(s1.equalsIgnoreCase("wrl"))
                return "x-world/x-vrml";
            if(s1.equalsIgnoreCase("vrml"))
                return "x-world/x-vrml";
            if(s1.equalsIgnoreCase("wml"))
                return "text/vnd.wap.wml";
            if(s1.equalsIgnoreCase("wmlc"))
                return "application/vnd.wap.wmlc";
            if(s1.equalsIgnoreCase("wmls"))
                return "text/vnd.wap.wmlscript";
            if(s1.equalsIgnoreCase("wmlsc"))
                return "application/vnd.wap.wmlscriptc";
            if(s1.equalsIgnoreCase("wbmp"))
                return "image/vnd.wap.wbmp";
            if(s1.equalsIgnoreCase("css"))
                return "text/css";
            if(s1.equalsIgnoreCase("jad"))
                return "text/vnd.sun.j2me.app-descriptor";
            if(s1.equalsIgnoreCase("jar"))
            {
                //debeEntrarAZip = true;
                return "application/java-archive";
            }
            if(s1.equalsIgnoreCase("3gp"))
                return "video/3gp";
            if(s1.equalsIgnoreCase("mp4"))
                return "video/3gpp";
        }
        return "application/octet-stream";
    }    
}
